layui.use(['form', 'table','layuimini','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var MapMerchantInfo = {
        id: 'MapMerchantInfoTable'
    };

    var column = [
        [
            {type: 'numbers', title: '序号'},
            {field: 'dataId', title: '数据ID', align: 'center'},
            {field: 'province', title: '省份', align: 'center'},
            {field: 'name', title: '商家名称', align: 'center'},
            {field: 'city', title: '城市', align: 'center'},
            {field: 'area', title: '区/县', align: 'center'},
            {field: 'searchArea', title: '搜索位置', align: 'center'},
            {field: 'address', title: '地址', align: 'center'},
            {field: 'location', title: '定位', align: 'center'},
            {field: 'tel', title: '电话', align: 'center'},
            {field: 'infoUpdateTime', title: '信息最后更新时间', align: 'center'},
            {field: 'keyword', title: '关键词', align: 'center'},
            {field: 'type', title: '行业类型', align: 'center'},
            {field: 'dataSource', title: '数据来源', align: 'center',
                templet: function (object) {
                    if('gaode' == object.dataSource){
                        return '<span class="layui-badge layui-bg-blue">高德地图</span>';
                    }else if('baidu' == object.dataSource){
                        return '<span class="layui-badge layui-bg-red">百度地图</span>';
                    }
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            // {field: 'updateby', title: '更新者', minWidth: 120, align: 'center'},
            // {field: 'createDate', title: '创建时间', minWidth: 120, align: 'center'},
            {templet: '#stateBtn', width: 170, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + MapMerchantInfo.id,
        url: '/map/mapMerchantInfo/recycleList',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    /**
     * 点击彻底删除按钮
     * @param id
     */
    MapMerchantInfo.thoroughlyDelete = function(id){
        layer.open({
            title: '彻底删除信息'
            ,content: '是否确定彻底删除该信息？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/map/mapMerchantInfo/thoroughlyDelete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(MapMerchantInfo.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    /**
     * 点击恢复按钮
     * @param id
     */
    MapMerchantInfo.restore = function(id){
        layer.open({
            title: '恢复信息'
            ,content: '是否确定恢复该信息？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/map/mapMerchantInfo/restore';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(MapMerchantInfo.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    //监听工具条
    table.on('tool(' + MapMerchantInfo.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'thoroughlyDelete') {
            MapMerchantInfo.thoroughlyDelete(data.id);
        }else if(layEvent === 'restore'){
            MapMerchantInfo.restore(data.id);
        }
    });

});